package cn.zhouchao.controller;

import cn.zhouchao.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getUserFallback" )
    public User getUser(@PathVariable("id") Long id){
        //String url = "http://localhost:10010/user/" + id;
        String url = "http://user-service/user/" + id;
        System.out.println("进入了接口方法-------order-getUser");
        // 此处就是Ribbon调用,此处如果采用不通那么就熔断
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }
    public User getUserFallback(@PathVariable("id") Long id){
        // 响应托底数据
        return new User(-1L, "触发熔断！","服务不可调用！");
    }
}
